// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  const db=cloud.database()
  const eventco=db.collection("event")

  try{
    return await eventco.add({
      data:{
        name:event.eventobj.name,
        profile:event.eventobj.profile,
        timefrom: new Date(event.eventobj.dtfrom + ' ' + event.eventobj.tmfrom + ' GMT+0800'),
        timeto: new Date(event.eventobj.dtto + ' ' + event.eventobj.tmto + ' GMT+0800'),
        place:event.eventobj.place,
        host:event.eventobj.id,
        picture:event.eventobj.picture,
        legal:false,
        count:0
      }
    })
  }
  catch(err){
    return err
  }

  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}